<template>
	<view>
		<view class="list">
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					公司名称
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入公司名称" placeholder-class="placeholder-class"
						v-model="dataForm.companyName" />
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					公司类型
				</view>
				<view class="acea-row row-middle row-right item-r sanjiao-right">
					<!-- <view class="no-data-r">请选择公司类型</view> -->
					<view :class="dataForm.companyType>0?'':'no-data-r'" @click="openPick('companyType')">
						{{dataForm.companyType?(chooseName(companyTypeDic,dataForm.companyType)):'请选择公司类型'}}
					</view>
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					所属行业
				</view>
				<view class="acea-row row-middle row-right item-r sanjiao-right" @click="chooseBussiner">
					<view :class="dataForm.businessName?'':'no-data-r'">
						{{dataForm.businessName?dataForm.businessName:'请选择所属行业'}} </view>
				</view>

			</view>
			<!-- 		<view class="acea-row row-middle row-between item border-b ">
				<view class="label">
					旗下品牌
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入旗下品牌" placeholder-class="placeholder-class"  v-model="childBrand" />
				</view>
			</view> -->
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					法人代表
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入法人代表" placeholder-class="placeholder-class"
						v-model="dataForm.legalPerson" />
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					注册资本
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入注册资本" placeholder-class="placeholder-class"
						v-model="dataForm.registeredCapital" />
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					公司规模
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入公司规模" placeholder-class="placeholder-class"
						v-model="dataForm.scale" />
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label">
					投资跨度
				</view>
				<view class="acea-row row-middle row-right item-r sanjiao-right">
					<view :class="investmentSpanDic[dataForm.investmentSpan]?'':'no-data-r'"
						@click="openPick('investmentSpan')">
						{{investmentSpanDic[dataForm.investmentSpan]?(chooseName(investmentSpanDic,dataForm.investmentSpan)):'请选择投资跨度'}}
					</view>
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					地址
				</view>
				<view class="acea-row row-middle row-right item-r address">
					<input type="text" placeholder="请输入/选择地址" v-model="dataForm.addres">
					<image src="../static/images/location.png" mode="" @click="chooseLocation"></image>
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label">
					公司网址
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入公司网址" placeholder-class="placeholder-class"
						v-model="dataForm.companyWebsite" />
				</view>
			</view>
			<!-- 公司简介 -->
			<view class="module">
				<view class="module-title">
					公司简介
				</view>
				<view class="textacea-box">
					<textarea placeholder="请填写公司简介" v-model="dataForm.enterpriseIntroduction" maxlength="-1"></textarea>
					<!-- <view class="num-text">
						{{supplementText.length}}/{{maxLength}}
					</view> -->
				</view>
			</view>

		</view>
		<view class="submit-btn" @click="toAddEneter">完成</view>
		<view class="uni-p-b-98"></view>
		<u-picker :defaultIndex="pickDefaultIndex" :show="pickShow" :columns="pickData" :keyName="pickKey"
			@cancel="pickShow = false" @confirm="pickChoose" @close="pickClose" :key="pickRefsh"></u-picker>

		<u-picker :show="businessShow" ref="uPicker" :columns="businessDic" keyName="name" @confirm="businessConfirm"
			@change="businessChangeHandler" @cancel="businessShow = false"></u-picker>
	</view>
</template>

<script>
	import {
		getBusiness,
		addEntert,
		editEnter,
		getEnterDetail
	} from '@/api/centerModule/index.js'
	import {
		dictionary
	} from '@/api/subletOfShop.js'
	export default {
		data() {
			return {
				dataForm: {
					companyName: '', //公司名称
					companyType: '',
					business: '', //所属行业
					businessName: '', //
					// childBrand:'',//旗下品牌
					legalPerson: '', //法人
					registeredCapital: '', //注册资本
					scale: '', //公司规模
					investmentSpan: -1, //投资跨度
					addres: '', //地址
					companyWebsite: '', //公司网址
					enterpriseIntroduction: '', //公司简介
					longitude: '', //经度
					latitude: '', //维度
				},
				companyTypeDic: [],
				/**
				 * 公共选中
				 */
				pickShow: false,
				pickData: [],
				pickDefaultIndex: [0],
				pickKey: 'name',
				pickFiled: '',
				pickRefsh: '',
				pickChooseD: {},
				investmentSpanDic: [{
					id: 0,
					name: '单一品牌经营',
				}, {
					id: 1,
					name: '多品牌经营',
				}, {
					id: 2,
					name: '多行业投资',
				}],
				businessShow: false,
				businessDic: [],
				defaultBusinessIndex: [],
				requireFiledRule: [{
						filed: 'companyName',
						tip: '请输入公司名称'
					},
					{
						filed: 'companyType',
						tip: '请选择公司类型'
					}, {
						filed: 'business',
						tip: '请选择所属行业'
					}, {
						filed: 'legalPerson',
						tip: '请输入法人代表'
					}, {
						filed: 'registeredCapital',
						tip: '请输入注册资本'
					}, {
						filed: 'scale',
						tip: '请输入公司规模'
					}, {
						filed: 'addres',
						tip: '请选择地址'
					}
				],
				isDetail: false
			};
		},
		created() {
			this.getHouseTypeDic();

		},
		methods: {
			openPick(filed, key) {
				key ? this.pickKey = key : null;
				this.pickFiled = filed;
				this.pickData = [];
				this.pickDefaultIndex = this.pickChooseD[this.pickFiled] ? this.pickChooseD[this.pickFiled] : [0];
				if (filed == 'companyType') {
					this.pickData = [this.companyTypeDic];
				} else if (filed == 'investmentSpan') {
					this.pickData = [this.investmentSpanDic]
				}
				this.pickShow = true;
			},
			pickChoose(data) {
				this.pickChooseD[this.pickFiled] = data?.indexs;
				this.dataForm[this.pickFiled] = data?.value[0].id;
				this.pickShow = false;
			},
			chooseName(dic, val) {
				return dic?.filter(item => item.id == val)[0]?.name;
			},
			pickClose() {
				console.log('close')
				this.pickRefsh = new Date().getTime()
			},
			//获取户型字典
			async getHouseTypeDic() {
				let res = await dictionary({
					gids: '25'
				})
				this.companyTypeDic = res.data['25']

			},
			//获取所属行业
			getBusinessMeh() {
				getBusiness().then(res => {
					console.log(res)
					this.businessDic = [res.data]
					this.$nextTick(() => {
						this.$refs.uPicker.setColumnValues(1, this.businessDic[0][0].children)
					})
					console.log(this.businessDic)
				})

			},
			businessConfirm(e) {
				console.log("confiner", e)
				const {
					value
				} = e;
				this.dataForm.business = [value[0].id, value[1].id].join('/')
				this.dataForm.businessName = [value[0].name, value[1].name].join('/')
				// console.log(this.dataForm.businessName)
				this.businessShow = false;
			},
			businessChangeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					indexs,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				console.log(e)
				this.defaultBusinessIndex = indexs;
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, value[0].children)
				}
			},
			chooseBussiner() {
				this.businessShow = true;
				if (this.businessDic.length <= 0) {
					this.getBusinessMeh()
				}
			},
			//选择地址
			chooseLocation() {
				let initlocation = {};
				if (this.dataForm.latitude && this.dataForm.longitude) {
					initlocation.latitude = this.dataForm.latitude;
					initlocation.longitude = this.dataForm.longitude;
				}
				uni.chooseLocation({
					...initlocation,
					success: (res) => {
						this.dataForm.addres = res.name;
						this.dataForm.longitude = res.longitude;
						this.dataForm.latitude = res.latitude;
					},
				})
			},
			/**
			 * 非空字段验证
			 * @param {Object} data 验证的对象数据 
			 * @param {Array} rule 自定义验证规则 
			 * rule:{
			 *  filed 字段名
			 *  isRequire 是否必填
			 *} 
			 */
			customValidation(data, rule) {
				console.log(data, rule)
				if (!rule) {
					return false;
				}
				for (let i = 0; i < rule.length; i++) {
					let k = rule[i].filed
					let kType = typeof data[k];
					console.log(k, kType)
					if (kType === 'string' && (!data[k])) {
						this.$util.Tips({
							title: rule[i].tip
						})
						return false;
					} else if (kType === 'number') {

						//包含0 
						if (!(rule[i].isZero)) {
							if ((data[k] === 0) && (!data[k])) {
								this.$util.Tips({
									title: rule[i].tip
								})
								return false;
							}
						} else {
							if (!data[k]) {
								this.$util.Tips({
									title: rule[i].tip
								})
								return false;
							}
						}
					} else if (kType == 'object') {
						if ((data[k] == null) || (data[k] && Object.keys(data[k]).length <= 0)) {
							this.$util.Tips({
								title: rule[i].tip
							})
							return false;
						}
					}
				}
				return true;
			},
			//提交企业
			async toAddEneter() {
				let validate = this.customValidation(this.dataForm, this.requireFiledRule)
				if (validate) {
					let submitData = Object.assign(this.dataForm, {});
					// submitData['city'] = this.$city;
					// submitData['floor'] = submitData['floor'].join("-")
					console.log(submitData)

					let result = null;
					if (this.isDetail) {
						result = await editEnter(submitData)
					} else {
						result = await addEntert(submitData);
					}
					if (result.code == 200) {
						const eventChannel = this.getOpenerEventChannel();
						eventChannel.emit('upList');
						uni.navigateBack(-1);
					}
				}
			},
			//获取企业详情
			getEnterDetailMeth(id) {
				console.log(id)
				getEnterDetail(id).then(res => {
					console.log(res.data)
					let {
						data
					} = res;
					this.dataForm = data;
				})
			}
		},
		onLoad(props) {
			if (props.id) {
				this.isDetail = true;
				this.getEnterDetailMeth(props.id);
			}
		}
	}
</script>

<style lang="scss">
	@import '/static/css/releaseForm.css';
</style>